﻿Imports System.Reflection
Imports System.IO

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
        Dim SQLiteFilePath As String = Path.GetTempPath & "System.Data.SQLite.dll"

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            'With Assembly.GetExecutingAssembly().GetManifestResourceStream("NWTCompiler.System.Data.SQLite.dll")
            '    'Using SQLiteFile As Stream = New FileStream(SQLiteFilePath, FileMode.Create)
            '    '    Dim test = IO.File.Create(SQLiteFilePath, 1024, FileOptions.DeleteOnClose)

            '    '    .CopyTo(SQLiteFile)
            '    'End Using
            '    Using SQLiteFile As Stream = IO.File.Create(SQLiteFilePath, 1024, FileOptions.DeleteOnClose)
            '        .CopyTo(SQLiteFile)

            '    End Using
            'End With

            AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf HandleAssemblyResolver
            'AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf HandleProcessExit
        End Sub

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            RemoveHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf HandleAssemblyResolver
        End Sub

        Private Function HandleAssemblyResolver(ByVal sender As Object, ByVal e As System.ResolveEventArgs) As Reflection.Assembly

            If e.Name.Contains("System.Data.SQLite") Then
                'With Assembly.GetExecutingAssembly().GetManifestResourceStream("NWTCompiler.System.Data.SQLite.dll")
                '    Using SQLiteFile As Stream = New FileStream(SQLiteFilePath, FileMode.Create)
                '        Dim test = IO.File.Create(SQLiteFilePath, 1024, FileOptions.DeleteOnClose)

                '        .CopyTo(SQLiteFile)
                '    End Using
                '    Using SQLiteFile As Stream = IO.File.Create(SQLiteFilePath, 1024, FileOptions.DeleteOnClose,fileSecurity.)
                '        .CopyTo(SQLiteFile)
                '        myReturn = Assembly.LoadFrom(SQLiteFilePath)
                '    End Using

                '    Return myReturn

                'End With

                Return Assembly.LoadFrom("System.Data.SQLite.dll")

                'Return Reflection.Assembly.Load(My.Resources.System_Data_SQLite)
            Else
                Return Nothing
            End If

            'Dim objExecutingAssemblies As Assembly
            'Dim assemblyData() As Byte

            'objExecutingAssemblies = Assembly.GetExecutingAssembly
            'Dim arrReferencedAssemblyNames = objExecutingAssemblies.GetReferencedAssemblies

            'For Each strAssemblyName As AssemblyName In arrReferencedAssemblyNames
            '    If strAssemblyName.FullName.Substring(0, strAssemblyName.FullName.IndexOf(",")) = e.Name.Substring(0, e.Name.IndexOf(",")) Then
            '        Dim stream = Assembly.GetExecutingAssembly.GetManifestResourceStream(String.Format("NWTCompiler.{0}.dll", New AssemblyName(e.Name).Name))

            '        'ReDim assemblyData(stream.Length)
            '        'stream.Read(assemblyData, 0, assemblyData.Length)
            '    End If
            'Next

            'Return Assembly.Load(assemblyData)

        End Function

        Private Function HandleProcessExit(ByVal sender As Object, ByVal e As System.ResolveEventArgs) As Reflection.Assembly
            File.Delete(SQLiteFilePath)

            Return Nothing

        End Function


    End Class

End Namespace

